from selenium.webdriver.common.by import By
from selenium import webdriver
from UIAutoTest.shop_test.Test_Shop import TestShop


# 封装通用操作
class BasePage(TestShop):

    def get_url(self, url):
        self.driver.get(url)

    # 输入
    def send_keys(self, selector, content):
        self.driver.find_element(*selector).send_keys(content)

    # 点击
    def click(self, selector):
        self.driver.find_element(*selector).click()

    # 滚动条
    def scrollTo(self):
        js = "window.scrollTo(0,1000);"
        self.driver.execute_script(js)

    # 滚动到指定元素
    def scrollTo_plus(self):
        element = self.driver.find_element(By.XPATH, '//*[@class="am-form form-validation nav-buy"]/div/button')  # 元素定位
        self.driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});", element)

    # 切换句柄
    def switch_to(self):
        # 获取所有窗口的句柄
        handleList = self.driver.window_handles
        # 切换窗口  handleList[-1]表示最新的句柄也就是最新的窗口，handleList[0]指的是第一个窗口
        self.driver.switch_to_window(handleList[-1])
